/*
 * @(#) $Header$
 *
 * Copyright (C) 2006  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.swing.filechooser;

import java.text.MessageFormat;
import java.util.ResourceBundle;
import ca.forklabs.baselib.swing.filechooser.ExtensionFileFilter;
import ca.forklabs.baselib.util.CharsetPropertiesControl;

/**
 * Class {@link ImmutableExtensionFileFilter} prevents modifications on the
 * filter.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.swing.filechooser.ImmutableExtensionFileFilter">Daniel Léonard</a>
 * @version $Revision$
 */
public class ImmutableExtensionFileFilter extends ExtensionFileFilter {

//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor
    * @param   description   the description of the filter.
    * @param   extensions    the desired extensions.
    */
   public ImmutableExtensionFileFilter(String description, String... extensions) {
      super(description);

   // extension needs to be added by calling super.addExtension()
   // otherwise we will blow ourselves
      for (String extension : extensions) {
         super.addExtension(extension);
         }
      }


//---------------------------
// Overridden methods from ca.forklabs.baselib.swing.filechooser.ExtensionFileFilter
//---------------------------

   /**
    * No extensions can be added after construction.
    * @param   extension   ignored.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   public final void addExtension(String extension) {
      String message = this.getCannotAddExtensionErrorMessage();
      throw new UnsupportedOperationException(message);
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the formatted error message about the fact that the writer is closed.
    * @return   the formatted error message.
    */
   protected String getCannotAddExtensionErrorMessage() {
      String key = ImmutableExtensionFileFilter.IMMUTABLE_EXTENSION_FILE_FILTER_CANNOT_ADD_EXTENSION;
      String message = ImmutableExtensionFileFilter.getLocalizedString(key);
      return message;
      }


//---------------------------
// Localization
//---------------------------

   /** The key for a change in the PFM default decoding parameter. */
   @SuppressWarnings("nls")
   public static final String IMMUTABLE_EXTENSION_FILE_FILTER_CANNOT_ADD_EXTENSION = "immutable.extension.file.filter.cannot.add.extension";

   /**
    * Gets the resource bundle itself.
    * @return   the resource bundle.
    */
   protected static ResourceBundle getResourceBundle() {
      String name = ImmutableExtensionFileFilter.class.getName();
      ResourceBundle bundle = ResourceBundle.getBundle(name, CharsetPropertiesControl.UTF_8_CONTROL);
      return bundle;
      }

   /**
    * Gets and formats the specified localized string from the menu resource
    * bundle.
    * @param   key   the key.
    * @param   arguments   the arguments to format the string.
    * @return   the value.
    */
   private static String getLocalizedString(String key, Object... arguments) {
      ResourceBundle bundle = ImmutableExtensionFileFilter.getResourceBundle();
      String pattern = bundle.getString(key);
      String message = MessageFormat.format(pattern, arguments);
      return message;
      }

   }
